package com.cn.lycss.service.impl;

import com.cn.lycss.dao.UserDao;
import com.cn.lycss.dao.impl.UserImpl;
import com.cn.lycss.model.ResultModel;
import com.cn.lycss.model.UserModel;
import com.cn.lycss.service.UserService;
import com.cn.lycss.util.JDBC;

import java.util.List;

public class UserServiceImpl implements UserService {

    @Override
    public ResultModel login(UserModel model) {
        ResultModel resultModel = new ResultModel();
        if (model.getSession().equals(model.getCode())) {

            UserDao dao=new UserImpl();
        UserModel userModel=dao.findByUserNameAndPassword(model);
            boolean  isHave=userModel !=null;
            if (isHave){

                resultModel.setMsg("success");
            }else{
                resultModel.setMsg("error");
            }
        } else {

            resultModel.setMsg("error-code");

        }
        return resultModel;
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
        ResultModel resultModel=new ResultModel();
        UserDao dao=new UserImpl();
        List<UserModel> list=dao.findAll(userModel);
        resultModel.setData(list);
        return resultModel;
    }

    @Override
    public ResultModel add(UserModel userModel) {
       ResultModel resultModel=new ResultModel();
       UserDao userDao=new UserImpl();
      int count= userDao.add(userModel);
       if (count>0){
           resultModel.setMsg("success");
       }else {
           resultModel.setMsg("error");
       }

        return resultModel;
    }

    @Override
    public ResultModel findById(UserModel userModel) {
        ResultModel resultModel=new ResultModel();
        UserDao dao=new  UserImpl();
       UserModel model=dao.findById(userModel);
       resultModel.setData(model);
        return resultModel;
    }

    @Override
    public ResultModel update(UserModel userModel) {
        ResultModel resultModel=new ResultModel();
        UserDao userDao=new UserImpl();
        int count= userDao.update(userModel);
        if (count>0){
            resultModel.setMsg("success");
        }else {
            resultModel.setMsg("error");
        }

        return resultModel;
    }


    public ResultModel del(UserModel userModel) {
        return null;
    }


    public ResultModel enable(UserModel userModel) {
        return null;
    }


    public ResultModel delete(UserModel userModel) {
        ResultModel resultModel=new ResultModel();
        UserDao userDao=new UserImpl();
        int count= userDao.delete(userModel);
        if (count>0){
            resultModel.setMsg("success");
        }else {
            resultModel.setMsg("error");
        }

        return resultModel;
    }

    @Override
    public ResultModel getCount(UserModel userModel) {
        ResultModel resultModel=new ResultModel();
        UserDao userDao = new UserImpl();
        resultModel.setCount(userDao.getCount(userModel));
        return resultModel;
//        return ResultModel.getInstance(userDao.getCount(userModel));
    }

    public static void main(String[] args) {
        UserModel userModel=new UserModel();

        new UserServiceImpl().findById(userModel);
    }
}
